/********************************************************************************
 * UsersAboutMeAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 16, 2008 11:21:13 AM by Ankur Jain
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.UserAboutMe;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

/**
 * The Class UsersAboutMeAction.
 */
public class UsersAboutMeAction extends FrontBaseSupport implements ServletRequestAware {

	public UsersAboutMeAction() {
		super(true, false);
		setMyAccount(true);
	}

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(UsersAboutMeAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The request. */
	private HttpServletRequest request;

	/** The user about me. */
	private UserAboutMe userAboutMe;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@SuppressWarnings("unchecked")
	public String execute() throws Exception{
		try {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("user.id", SessionUtil.getUser(request)
					.getId());
			List<UserAboutMe> userAboutMeList = (List<UserAboutMe>) CommonFunctionDelegator
					.getObjectBySearchCriteria(searchCriteria,
							UserAboutMe.class);
			this.userAboutMe = (userAboutMeList.size() > 0) ? userAboutMeList
					.get(0) : null;
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String aboutMe() throws Exception{
		setLeftAllowed(false);
		if(request.getParameter("userId") != null) {
			try {
				userAboutMe = (UserAboutMe) CommonFunctionDelegator.getObjectById("user.id", UserServiceDelegator.getUserByUserId(request.getParameter("userId")).getId(), UserAboutMe.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}

	/**
	 * Save about me.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String saveAboutMe() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("saveOption() - start"); //$NON-NLS-1$
		}
		try{
			if(this.userAboutMe.getId() == null) {
				this.userAboutMe.setUser(SessionUtil.getUser(request));
				CommonFunctionDelegator.generalInsertbyobj(this.userAboutMe);
			} else {
				this.userAboutMe.setUser(SessionUtil.getUser(request));
				CommonFunctionDelegator.generalUpdatebyobj(this.userAboutMe);
			}
		}catch (Exception e) {
			addActionError("Error in saving about me page.");
			return INPUT;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("saveOption() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Gets the user about me.
	 *
	 * @return the user about me
	 */
	public UserAboutMe getUserAboutMe() {
		return userAboutMe;
	}

	/**
	 * Sets the user about me.
	 *
	 * @param userAboutMe the new user about me
	 */
	public void setUserAboutMe(UserAboutMe userAboutMe) {
		this.userAboutMe = userAboutMe;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}